package com.android.pagemarket.ui.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;

import com.android.pagemarket.ui.activity.main.DialogShowActivity;
/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-2
 * @see : 监听网络状态
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class NetworkStateReceiver extends BroadcastReceiver
{
	@Override
	public void onReceive(Context context, Intent intent)
	{
		State wifiState = null, mobileState = null;
		ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);			
		mobileState = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
		wifiState = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
		
		if(State.CONNECTING == mobileState || State.CONNECTING == wifiState)
		{
		    // 正在连接中，do nothing ...
		}
		else if ( State.DISCONNECTED == mobileState && State.DISCONNECTED == wifiState)
		{
			// 网络无效 进行提示并提示是否打开网络设置
			DialogShowActivity.startActivity(context, DialogShowActivity.DIALOG_NETWORK);
		}
		else if (State.CONNECTED == mobileState)
		{
			// 网络连接成功
		    context.startService(new Intent(PushService.NET_ENABLE_ACTION));
		}
		else if (State.CONNECTED == wifiState)
		{
			// 网络连接成功
		    context.startService(new Intent(PushService.NET_ENABLE_ACTION));
		}
	}
}